<?php

// +----------------------------------------------------------------------
// | 插件后台管理控制器
// +----------------------------------------------------------------------
// | Author: Kai <maifakai@126.com>
// +----------------------------------------------------------------------

namespace plugins\SmsMsg\controller;
use app\common\builder\TKuitpl;
use app\common\controller\Common;
use plugins\SmsMsg\model\AlidayuSms;
require_once(dirname(dirname(__FILE__))."/common.php"); //引入插件函数库

/**
 * 插件后台管理控制器
 * @package plugins\Alidayu\controller
 */
class Admin extends Common
{
    /**
    * 插件管理方法
    * @Author: Kai <maifakai@126.com>
    */
    public function index()
    {
        // 查询条件
        $map = $this->getMap();
        $order = $this->getOrder();
        $data_list = AlidayuSms::where($map)->order($order)->paginate();
        // 分页数据
        $page = $data_list->render();
        // 自定义按钮
        $btnConfig = [
            'title' => '插件配置',
            'icon' => 'fa fa-cogs',
            'href' => url('plugin/config', array('name'=>'SmsMsg')),
            'class' => 'btn btn-info'
        ];
        $btnBack = [
            'title' => '返回插件列表',
            'icon' => 'fa fa-reply',
            'href' => url('plugin/index'),
            'class' => 'btn btn-warning'
        ];
        $btnClear = [
            'title' => '清空所有记录',
            'icon' => 'fa fa-trash',
            'href' => plugin_url('SmsMsg/Admin/delall'),
            'class' => 'btn btn-default ajax-get confirm'
        ];
        return TKuitpl::make('table')
            ->setPageTips("<p><b># 插件调用方法 #</b></p><p>短信发送方法: plugin_action('Alidayu/SmsMsg/send', ['mobile' => '接收人手机号', 'param' => ['code'=>'验证码','time'=>'分钟数'], 'template' => '模版ID默认空']);</p>", 'success')
            ->setPageTitle('短信记录')
            ->addFilter('mobile,sign_name,template_code')
            ->addTimeFilter('create_time')
            ->addOrder('id,create_time')
            ->setSearch(['id' => 'ID', 'mobile' => '接收手机号', 'sign_name' => '短信签名'])
            ->addColumn('id', 'ID')
            ->addColumns([
                ['mobile', '接收手机号'],
                ['sign_name', '短信签名'],
                ['template_code', '短信模版ID'],
                ['date_json', '请求数据'],
                ['create_time', '发送时间', 'datetime'],
                ['update_time', '送达时间', 'datetime'],
                ['status', '状态', 'callback', 'format_send_status', 'true'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('custom', $btnBack)
            ->addTopButton('custom', $btnConfig)
            ->addTopButtons('delete')
            ->addTopButton('custom', $btnClear)
            ->addRightButtons('delete')
            ->setTableName('plugin_alidayu_sms')
            ->setRowList($data_list)
            ->setPages($page)
            ->fetch();
    }
    
    /**
    * 清空所有短信记录
    * @author: Kai <maifakai@126.com>
    */
    public function delall(){
        if (db('plugin_smsmsg_sms')->where('id>=0')->delete()){
            $this->success('已清空');
        }else{
            $this->error('操作失败');
        }
    }
}